import { Injectable, NestMiddleware, Logger } from '@nestjs/common';
import { Request, Response } from 'express';
import { Buffer } from 'buffer';

@Injectable()
export class DataMiddleware implements NestMiddleware {
  constructor() {}

  use(req: Request, res: Response, next: () => void) {
    // Logger.log(req.body);
    if (Buffer.isBuffer(req.body)) {
      try {
        req.body = JSON.parse(req.body.toString('utf8'));
        // console.log(req.body)
      } catch (error) {
        Logger.log(error.message);
        // console.log(error.message)
      }
    }
    next();
  }
}
